﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using bs_ShowRoom;

namespace ShowRoom.admin
{
    public partial class imgSlideManager : System.Web.UI.Page
    {
        private const string strNoData = "No Data Found!";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hplLogo.Style["cursor"] = "hand";
                hplLogo.Attributes.Add("onclick", "OpenAddNewImg()");
                GetImg();
            }
            if (txtDeleteImgSlide.Value != "")
            {
                string Type_ = "2";

                fnInsUpdImg(txtDeleteImgSlide.Value, "", "", "", "","","", "2");

                GetImg();
            }
        }
        private void GetImg()
        {
            DataTable dt = GrtLogoHopTac("", "", "");
            gvSlide.DataSource = dt;
            int count1 = dt.Rows.Count;
            if (count1 == 0)
            {
                dt.Rows.Add(dt.NewRow());
            }
            gvSlide.DataBind();
            gvSlide.GridLines = GridLines.None;
            if (count1 == 0)
            {
                int columns = gvSlide.Columns.Count;
                gvSlide.Rows[0].Cells[0].Text = strNoData;
                gvSlide.Rows[0].Cells[0].ColumnSpan = columns;
                for (int i = 1; i < columns; i++)
                {
                    gvSlide.Rows[0].Cells.RemoveAt(1);
                }
            }
        }

        private string fnInsUpdImg(string img_id, string img_name, string picture, string active, string link_web, string user_id, string note, string myType)
        {
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("img_id", img_id);
            myMsg.CreateFirstNode("img_name", img_name);
            myMsg.CreateFirstNode("picture", picture);
            myMsg.CreateFirstNode("active", active);
            myMsg.CreateFirstNode("link_web", link_web);
            myMsg.CreateFirstNode("user_id", user_id);
            myMsg.CreateFirstNode("note", note);
            myMsg.CreateFirstNode("Type", myType);

            bs_info mybs = new bs_info(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());

            string strXML = myMsg.GetXmlStr;
            string strReturn = mybs.DoReturnStr("INSUPDIMGSLIDE", strXML, "");
            return strReturn;
            //*****************************************************************************
        }

        private DataTable GrtLogoHopTac(string img_id, string img_name, string active)
        {
            bs_info mybs = new bs_info(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("img_id", img_id);
            myMsg.CreateFirstNode("img_name", img_name);
            myMsg.CreateFirstNode("active", active);
            DataSet ds = mybs.DoReturnDataSet("GETIMGSLIDEADMIN", myMsg.GetXmlStr, "");
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            return dt;

        }

        protected void gvSlide_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string img_id = drv["img_id"].ToString().Trim();

                ((Label)e.Row.FindControl("lbldateadd")).Text = GetDateTimeStr14(((Label)e.Row.FindControl("lbldateadd")).Text);

                ((Image)e.Row.FindControl("imgDelete")).Style["cursor"] = "hand";
                ((Image)e.Row.FindControl("imgDelete")).Attributes.Add("onclick", "CheckDeleteImgSlide('" + img_id + "')");

                ((Image)e.Row.FindControl("imgEdit")).Style["cursor"] = "hand";
                ((Image)e.Row.FindControl("imgEdit")).Attributes.Add("onclick", "OpenNewEditImg('" + img_id + "')");
                
                string active = ((Label)e.Row.FindControl("lblactive")).Text.Trim();
                if (active == "Y")
                    ((Label)e.Row.FindControl("lblactive")).Text = "<img src='img/active.png' />";
                else
                    ((Label)e.Row.FindControl("lblactive")).Text = "<img src='img/inactive.png' />";
            }
        }
        public static string GetDateTimeStr14(string date)
        {
            string str = date;
            if (date.Length >= 14)
                str = date.Substring(0, 4) + "/" + date.Substring(4, 2) + "/" + date.Substring(6, 2) + " " + date.Substring(8, 2) + ":" + date.Substring(10, 2) + ":" + date.Substring(12, 2);

            return str;
        }
        public static string GetDateTimeStr8(string date)
        {
            string str = date;
            if (date.Length >= 8)
                str = date.Substring(0, 2) + "/" + date.Substring(2, 2) + "/" + date.Substring(4, 4);

            return str;
        }

    }
}